package com.boy.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boy.entity.Account;
import com.boy.mapper.AccountMapper;
import com.boy.service.AccountService;
import com.boy.tool.domain.Pos;
import com.boy.tool.annotate.RequirePermissions;
import com.boy.tool.domain.ResultVo;
import com.boy.tool.annotate.WhdLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author whd
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountMapper accountMapper;
    @WhdLog
    @RequirePermissions({"Account:create"})
    @PostMapping("/create")
    public ResultVo create(@RequestParam String name, @RequestParam String phone, @RequestParam String type, @RequestParam String roles) {
        Account account = accountService.created(phone, name, type, roles, "");
        return ResultVo.success("添加成功", account);
    }

    @WhdLog
    @RequirePermissions({"Account:update"})
    @PostMapping("/update")
    public ResultVo update(@RequestBody Account account) {
        if (accountService.updateById(account))
            return ResultVo.success(account);
        return ResultVo.failure();
    }

    @PostMapping("/findAll")
    public ResultVo find() {
        return ResultVo.success(accountMapper.findAll());
    }

    @RequirePermissions({"Account:find"})
    @PostMapping("/find")
    public ResultVo find(@RequestBody Pos pos) {
        String name = (String) pos.getParam().get("name");
        String phone = (String) pos.getParam().get("phone");
        IPage<Account> accountIPage = accountService.find(pos, name, phone);
        return ResultVo.success(accountIPage);
    }


    @RequirePermissions({"Account:get"})
    @PostMapping("/get")
    public ResultVo get(@RequestParam String id) {
        Account account = accountService.getById(id);
        if (account == null) return ResultVo.failure();
        return ResultVo.success(account);
    }

    @WhdLog
    @RequirePermissions({"Account:delete"})
    @PostMapping("/delete")
    public ResultVo delete(@RequestParam String id) {
        if (accountService.removeById(id)) return ResultVo.success("删除成功");
        return ResultVo.failure();
    }

}

